import 'dart:io';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/viewmodel/patrol/offline_patrol_list_view_model.dart';

import '../../res/colors.dart';
import 'offline_patrol_point_list_widget.dart';

class OfficeTaskListPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _OfficeTaskListState();
}

class _OfficeTaskListState
    extends BaseMvvmState<OfflinePatrolListViewModel, OfficeTaskListPage> {
  @override
  Widget buildLoadingContentView() {
    return viewModel.taskList.isEmpty
        ? Center(
            child: TextButton(
            onPressed: () async {
              await viewModel.getOfflineTask();
            },
              style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(Colors.blue)
              ),
            child: Text('获取今日任务',style: TextStyle(color: Colors.white),),
          ))
        : DefaultTabController(
            length: viewModel.taskList.length,
            child: Stack(
              children: [
                Column(
                  children: [
                    topUnPatrolWidget(),
                    TabBar(
                        isScrollable: true,
                        indicatorColor: Colours.app_main,
                        unselectedLabelColor: Colors.black,
                        labelColor: Colours.app_main,
                        labelPadding: EdgeInsets.symmetric(horizontal: 10),
                        indicatorSize: TabBarIndicatorSize.label,
                        tabs: viewModel.taskList
                            .map((e) => Tab(
                                  text: "${e.name}\n${e.timeRange}",
                                ))
                            .toList()),
                    Container(
                      height: 20,
                    ),
                    TabBarView(
                            children: viewModel.taskList
                                .map((e) => OfflinePatrolPointListWidget(
                                      taskId: e.id,
                                    ))
                                .toList())
                        .setWeight(1)
                  ],
                ).setLocation(left: 0, top: 0, right: 0, bottom: 0),
                Row(
                  children: [
                    if (Platform.isIOS) ...[
                      TextButton(
                        onPressed: () {
                          viewModel.startNfc();
                        },
                        child: CommonText('读取NFC',
                            textSize: 17, textColor: Colors.white),
                        style: ButtonStyle(
                            backgroundColor:
                                WidgetStateProperty.all(Colours.app_main),
                          shape: WidgetStateProperty.all(StadiumBorder())
                            ),
                      ).setWeight(1),
                      Container(
                        width: 10,
                      )
                    ],
                    TextButton(
                            onPressed: () {
                              viewModel.getOfflineTask();
                            },
                        style: ButtonStyle(
                            shape: WidgetStateProperty.all(StadiumBorder(side: BorderSide(color: Colors.blue)))
                        ),
                            child: Text("刷新任务"))
                        .setWeight(1),
                    Container(
                      width: 10,
                    ),
                    TextButton(onPressed: () {
                      viewModel.scanCode();
                    }, child: Text("扫码巡防",style: TextStyle(color: Colors.white),),
                    style: ButtonStyle(
                      backgroundColor: WidgetStateProperty.all(Colors.blue),
                        shape: WidgetStateProperty.all(StadiumBorder(side: BorderSide(color: Colors.blue)))
                    ),)
                        .setWeight(1),
                  ],
                ).setLocation(bottom: 16, left: 16, right: 16)
              ],
            ));
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text("今日巡防"),
        actions: [TextButton(onPressed: () {
          viewModel.selectRecordType();
        }, child: Text("巡防记录",style: TextStyle(color: Colours.app_main),))],
      ),
      body: loadingContentWidget,
    );
  }

  @override
  OfflinePatrolListViewModel createViewModel() {
    return OfflinePatrolListViewModel(context);
  }

  Widget topUnPatrolWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        CommonText('今日未打卡巡防点 '),
        CommonText((viewModel.unFinishNum ?? 0).toString(),
            fontWeight: FontWeight.w600, textColor: Colours.app_main)
            .addToContainer(
          alignment: Alignment.center,
          padding: EdgeInsets.all(5),
          decoration: BoxDecoration(
            color: Colors.transparent,
            borderRadius: 20.borderRadius,
            border: Border.all(width: 2, color: Colours.app_main),
          ),
          constraints: BoxConstraints(
              minHeight: 30,
              minWidth: 30
          ),
        ),
        CommonText(' 个'),
      ],
    ).addToContainer(
        decoration:
        BoxDecoration(color: Colors.white, borderRadius: 8.borderRadius),
        padding: EdgeInsets.all(8),
        margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10));
  }

  @override
  void onRetryClick() {
    viewModel.getOfflineTask();
  }
}
